class HomeBanner {
  String objectId;
  String bookid;
  int recommend;
  String url;
  String createdAt;
  String updatedAt;

  HomeBanner.fromJson(Map<String, dynamic> map)
      : bookid = map['bookid'],
        createdAt = map['createdAt'],
        objectId = map['objectId'],
        recommend = map['recommend'],
        updatedAt = map['updatedAt'],
        url = map['url'];
}

class Classify {
  String objectId;
  int idx;
  String des;
  String name;
  String createdAt;
  String updatedAt;

  Classify.fromJson(Map<String, dynamic> map)
      : objectId = map['objectId'],
        idx = map['idx'],
        des = map['des'],
        name = map['name'],
        createdAt = map['createdAt'],
        updatedAt = map['updatedAt'];
}

class Book {
  String objectId;
  int recommend; //越小，越靠前
  String platform; //平台 xmly、bear
  String platformbid; //平台对应id
  int price;
  String pic = "http://via.placeholder.com/360x360";
  String bookdes = "小智慧，大故事";
  String bookname = "智慧小故事";
  String createdAt;
  String updatedAt;

  int playCount = -1; //播放数
  int chapterCount = -1; //集数

  Book();

  Map<String, dynamic> toJson() {
    return {
      'objectId': objectId,
      'recommend': recommend,
      'platform': platform,
      'platformbid': platformbid,
      'price': price,
      'pic': pic,
      'bookdes': bookdes,
      'bookname': bookname,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
    };
  }

  Book.fromJson(Map<String, dynamic> map)
      : objectId = map['objectId'],
        recommend = map['recommend'],
        platform = map['platform'],
        platformbid = map['platformbid'],
        price = map['price'],
        pic = map['pic'],
        bookdes = map['bookdes'],
        bookname = map['bookname'],
        createdAt = map['createdAt'],
        updatedAt = map['updatedAt'];

  updateFromPlatformJson(Map<String, dynamic> map) {
    if (platform == "xmly") {
      platformbid = map['id'].toString();
//      pic = map['cover_url_middle'];
//      bookdes = map['album_intro'];
//      bookname = map['album_title'];
      playCount = map['play_count'];
      chapterCount = map['include_track_count'];
    }
  }

  bool isBear() {
    if (platform == null) {
      return true;
    }
    if (platform == "bear") {
      return true;
    }
    return false;
  }
}

class Chapter {
  String objectId;
  int chaptertime;
  int playcount;
  String text;
  String url; //下载地址
  String pic = "http://via.placeholder.com/360x360";
  String name = "小智慧，大故事";
  String des = "智慧小故事";
  String createdAt;
  String updatedAt;

  Chapter();

  String getTimeStr(double scale) {
    String ms = "00";
    String ss = "00";
    try {
      int m = 0;
      int s = 0;
      int c = (chaptertime * scale).toInt();
      if (c > 60) {
        m = c ~/ 60;
        s = c % 60;
      } else {
        m = 0;
        s = c;
      }
      ms = "$m";
      ss = "$s";
      if (m < 10) {
        ms = "0$m";
      }
      if (s < 10) {
        ss = "0$s";
      }
    } catch (e) {
      print("getTimeStr exception: $e");
    }
    return "$ms:$ss";
  }

  Map<String, dynamic> toJson() {
    return {
      'objectId': objectId,
      'chaptertime': chaptertime,
      'playcount': playcount,
      'text': text,
      'url': url,
      'pic': pic,
      'name': name,
      'des': des,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
    };
  }

  Map<String, dynamic> toPlayPlatformJson(String platform) {
    if ((platform == null || platform == "bear") && url != null && url.length > 0) {
      return {
        'trackTitle': name,
        'playUrl32': url,
        'playcount': playcount,
        'pic': pic,
        'duration': chaptertime,
        "trackIntro": des,
        'dataId': objectId.hashCode
      };
    } else if (platform == "xmly") {
      return {
        'trackTitle': name,
        "trackIntro": des,
        'duration': chaptertime,
        'playcount': playcount,
        'pic': pic,
        'dataId': int.parse(objectId)
      };
    }
    return {};
  }

  Chapter.fromJson(Map<String, dynamic> map)
      : objectId = map['objectId'],
        chaptertime = map['chaptertime'],
        playcount = map['playcount'],
        text = map['text'],
        url = map['url'],
        pic = map['pic'],
        name = map['name'],
        des = map['des'],
        createdAt = map['createdAt'],
        updatedAt = map['updatedAt'];

  Chapter.fromXmlyJson(Map<String, dynamic> map)
      : objectId = map['id'].toString(),
        chaptertime = map['duration'],
        playcount = map['play_count'],
//        text = map['track_title'],
//        url = map['url'],
        pic = map['cover_url_middle'],
        name = map['track_title'],
        des = map['track_intro'],
        createdAt = map['createdAt'],
        updatedAt = map['updatedAt'];

  Chapter.fromNowPlayListJson(Map<String, dynamic> map)
      : objectId = map['dataId'].toString(),
        chaptertime = map['duration'],
        playcount = map['playcount'],
        url = map['playUrl32'],
        pic = map['pic'],
        name = map['trackTitle'],
        des = map['trackIntro'];
}

class User {
  String objectId;
  String nickName = "贝尔${DateTime.now().millisecondsSinceEpoch}";
  String password = "123456";
  String phoneNumber;
  int learnTime = 0;
  int age = 0;
  String headUrl;
  String headCdn;
  int sex = 0; //0未知1男2女
  String createdAt;
  String updatedAt;

  User();

  User.fromJson(Map<String, dynamic> map)
      : objectId = map['objectId'],
        nickName = map['nickName'],
        password = map['password'],
        phoneNumber = map['phoneNumber'],
        learnTime = map['learnTime'],
        age = map['age'],
        headUrl = map['headUrl'],
        headCdn = map['headCdn'],
        sex = map['sex'],
        createdAt = map['createdAt'],
        updatedAt = map['updatedAt'];

  Map<String, dynamic> toJson() {
    return {
      'objectId': objectId,
      'nickName': nickName,
      'password': password,
      'phoneNumber': phoneNumber,
      'sex': sex,
      'age': age,
      'headUrl': headUrl,
      'headCdn': headCdn,
      'learnTime': learnTime
    };
  }

  Map<String, dynamic> toRegisterJson() {
    return {
      'nickName': nickName,
      'password': password,
      'phoneNumber': phoneNumber,
      'sex': sex,
      'age': age,
      'headurl': headUrl,
      'learnTime': learnTime,
    };
  }

  String getSexStr() {
    if (sex == 1) {
      return "男";
    } else if (sex == 2) {
      return "女";
    }
    return "未知";
  }
}

class UserCreate {
  String objectId;
  String sessionToken;
  String createdAt;

  UserCreate();

  UserCreate.fromJson(Map<String, dynamic> map)
      : objectId = map['objectId'],
        sessionToken = map['sessionToken'],
        createdAt = map['createdAt'];
}

class PlayList {
  int index;
  int playModel;
  String bookName;
  List<dynamic> list;

  PlayList();

  Map<String, dynamic> toJson() {
    return {'list': list, 'index': index, 'playModel': playModel, 'bookName': bookName};
  }

  PlayList.fromJson(Map<String, dynamic> map)
      : index = map['index'],
        playModel = map['playModel'],
        bookName = map['bookName'],
        list = map['list'];
}

String getPlayCountStr(int playCount) {
  double s = playCount.toDouble();
  if (s > 100000000) {
    return "${(s / 100000000).toStringAsFixed(1)}亿";
  } else if (s > 10000) {
    return "${(s / 10000).toStringAsFixed(1)}万";
  } else if (s > 1000) {
    return "${(s / 1000).toStringAsFixed(1)}千";
  } else {
    if (s < 0) {
      s = 0;
    }
    return "$s";
  }
}
